/*=====================================================================

  File:      WireframeFileStorage.cs
  Summary:   Provides I/O services for wireframes from files
  Date:	     May 27, 2006

---------------------------------------------------------------------

  This file is part of the Microsoft SQL Server Code Samples.
  Copyright (C) Microsoft Corporation.  All rights reserved.

This source code is intended only as a supplement to Microsoft
Development Tools and/or on-line documentation.  See these other
materials for detailed information regarding Microsoft code samples.

THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

======================================================= */

using System;
using System.IO;
using System.Diagnostics.CodeAnalysis;
using System.Collections.Generic;
using System.Globalization;

namespace Microsoft.Samples.SqlServer.DB3D
{

    // WireframeFileStorage provides I/O services for wireframes from files

    public static class WireframeFileStorage
    {
        // parse and load triangle information from a text file
        [SuppressMessage("Microsoft.Usage", "CA2201:DoNotRaiseReservedExceptionTypes",
                         Justification = "In order to keep the example simple, no custom exceptions are defined")]
        public static Wireframe Load(string filename, WireframeProgress progress) {

            // temporary storage for the triangles (we'll convert it
            // into Triangle[] at the end of the method)
            List<Triangle> list = new List<Triangle>();
            int i = 0;

            // each line in the file represents 3 3D points -> a triangle in space
            foreach(string line in File.ReadAllLines(filename)) {
                if(char.IsDigit(line[0]) || line[0] == '-') {
                    string[] pts = line.Split(' ');

                    if(pts.Length != 9)
                        throw new ApplicationException("Invalid file format: " + filename);

                    list.Add(new Triangle(
                        new Point3D(double.Parse(pts[0], CultureInfo.InvariantCulture), 
                        double.Parse(pts[1], CultureInfo.InvariantCulture), 
                        double.Parse(pts[2], CultureInfo.InvariantCulture)), 
                        new Point3D(double.Parse(pts[3], CultureInfo.InvariantCulture), 
                        double.Parse(pts[4], CultureInfo.InvariantCulture), 
                        double.Parse(pts[5], CultureInfo.InvariantCulture)), 
                        new Point3D(double.Parse(pts[6], CultureInfo.InvariantCulture), 
                        double.Parse(pts[7], CultureInfo.InvariantCulture), 
                        double.Parse(pts[8], CultureInfo.InvariantCulture))));

                    // if the user gave us a delegate, report progress every
                    // 100 triangles
                    i++;
                    if(null != progress && i % 100 == 0)
                        progress(i);
                }
            }

            // last call to the callback with i==total nr of triangles
            if(null != progress)
                progress(i);

            // create and initialize a new wireframe with the information
            // we loaded
            Wireframe wireframe = new Wireframe();
            wireframe.Triangles = list.ToArray();
            wireframe.Name = Path.GetFileNameWithoutExtension(filename);

            return wireframe;
        }
    }
}
